package com.example.demo;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter
{
	private LayoutInflater mInflater;
	private ArrayList<HashMap<String, Object>> listItem;
	private int selectedRow = -1;

	public ListAdapter(Context context,
			ArrayList<HashMap<String, Object>> listItem)
	{
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
	}

	public void setSource(ArrayList<HashMap<String, Object>> listItem)
	{
		this.listItem = listItem;
	}

	
	public int getCount()
	{
		// TODO Auto-generated method stub
		return listItem.size();
	}

	
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.items_row, null);
			holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
			holder.tv_name1 = (TextView) convertView.findViewById(R.id.tv_name1);
			holder.tv_name2 = (TextView) convertView.findViewById(R.id.tv_name2);
			holder.tv_email = (TextView) convertView.findViewById(R.id.tv_email);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.tv_id.setTextSize(16);
		holder.tv_name1.setTextSize(16);
		holder.tv_name2.setTextSize(16);
		holder.tv_email.setTextSize(16);
		

		holder.tv_id
				.setText(String.valueOf(listItem.get(position).get("Id")));
		holder.tv_name1.setText(String
				.valueOf(listItem.get(position).get("firstname")));
		holder.tv_name2.setText(String
				.valueOf(listItem.get(position).get("lastname")));
		holder.tv_email.setText(String.valueOf(listItem.get(position).get("email")));

		if (position == selectedRow)
		{// Color.rgb(242, 242, 242)
			holder.tv_id.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.tv_name1.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.tv_name2.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.tv_email.setBackgroundColor(Color.rgb(255, 219, 16));
		} else
		{
			holder.tv_id.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_name1.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_name2.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_email.setBackgroundColor(Color.rgb(204, 204, 204));
		}

		return convertView;
	}
	
	

	public final class ViewHolder
	{
		public TextView tv_id;
		public TextView tv_name1;
		public TextView tv_name2;
		public TextView tv_email;
	}

	public void ScrollToRow(int row)
	{
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex()
	{
		return selectedRow;
	}

}
